package com.ttg.utils.connection;

//~--- non-JDK imports --------------------------------------------------------

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * Class        HttpClientUtils
 * Description
 * Create       2015.08.15 at 14:53:34 CST
 * @author      Ardy
 */
public class HttpClientUtils {

    /**
     * Field logger
     * Description
     */
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);


    /**
     * Method sendPostHttpRequest
     * Description 发送POST请求
     *  @param    url String
     * @param   method String
     * @param    param Map
     * @return  String
     */
    public static String sendPostHttpRequest(String url, String method, Map param) {
        HttpRequest request = new HttpRequest(url);

        request.setParameters(param);
        request.setMethod((method == null) ? "POST" : method);

        String     responseStr = "";
        HttpClient httpClient  = null;

        try {
            httpClient  = HttpClient.get();
            responseStr = httpClient.requestWithStringResult(request, 1, false);
        } catch (TTGHttpClientException e) {
            e.printStackTrace();
        }

        logger.debug(responseStr);

        return responseStr;
    }

    /**
     * Method createLinkString
     * Description 按照字母的顺序进行网络参数拼接
     *
     * @param params Map<String,String> 参数的map集合
     *
     * @return String
     */
    public static String createLinkString(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());

        Collections.sort(keys);

        StringBuilder sb    = new StringBuilder();
        String        key   = null;
        String        value = null;

        for (int i = 0 ; i < keys.size() ; ++i) {
            key   = (String) keys.get(i);
            value = (String) params.get(key);
            sb.append(key).append("=").append(value);

            if (i < keys.size() - 1) {
                sb.append("&");
            }
        }

        return sb.toString();
    }

    public static String createLinkStringQ(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());

        Collections.sort(keys);

        StringBuilder sb    = new StringBuilder();
        String        key   = null;
        String        value = null;

        for (int i = 0 ; i < keys.size() ; ++i) {
            key   = (String) keys.get(i);
            value = (String) params.get(key);
            sb.append(key).append("=").append ("\"").append(value).append ("\"");

            if (i < keys.size() - 1) {
                sb.append("&");
            }
        }

        return sb.toString();
    }

    public static Map<String, String> paramFilter(Map<String, String> sArray,String signTypeParam,String signParam) {

        Map<String, String> result = new HashMap<String, String> ();

        if (sArray == null || sArray.size() <= 0) {
            return result;
        }

        for (String key : sArray.keySet()) {
            String value = sArray.get(key);
            if (value == null || value.equals("") || key.equalsIgnoreCase(signParam)
                    || key.equalsIgnoreCase(signTypeParam)) {
                continue;
            }
            result.put(key, value);
        }

        return result;
    }


    /**
     * Method main
     * Description
     *
     * @param   args String[]
     *
     * @throws TTGHttpClientException
     */
    public static void main(String[] args) throws TTGHttpClientException {}
}


//~ Formatted by Jindent --- http://www.jindent.com
